package box.tuson.com.tusonbox.circle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import box.tuson.com.tusonbox.circle.CanvasUtil;

public class CircleImageView extends ImageView {

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        //获取图片，转化为Bitmap
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        if (null == b) {
            return;
        }
        //将图片转为32位ARGB位图，保证图片质量
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        //获取图片的宽度
        int width = getWidth();
        Bitmap roundBitmap = CanvasUtil.getCirCleBitmap(bitmap, width);
        //重绘图片
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }
}

